package com.yqg.education.service.impl;

import com.yqg.education.entity.ExamSign;
import com.yqg.education.entity.Period;
import com.yqg.education.entity.TeachPlan;
import com.yqg.education.mapper.*;
import com.yqg.education.service.IExamSignService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ExamSignServiceImpl 服务实现类
 * @author 张芸博
 * @since 2025-04-10
 */
@Service
public class ExamSignServiceImpl extends ServiceImpl<ExamSignMapper, ExamSign> implements IExamSignService {
    @Resource
    private ExamLogMapper examLogMapper;
    @Resource
    private PeriodMapper periodMapper;
    @Resource
    private TeachPlanMapper teachPlanMapper;


    @Override
    public Map<String, Object> stuFindIsLearned(Integer courseId, Integer stuId) {
        final Boolean[] result = {true};
        // 判断是否完成视频的学习
        List<Period> periodList = periodMapper.findPeriodByCourseId(null, null, courseId, stuId);
        if (periodList != null) {
            periodList.forEach(period -> {
                if(period.getFlow() == null || period.getFlow().equals(0)){
                    result[0] = false;
                }
            });
        }
        // 判断资源是否已查看
        List<Long> courseIdList = new ArrayList<>();
        courseIdList.add((long) courseId);

        List<TeachPlan> resourceList = teachPlanMapper.findResourcePage(null,null,null,null,courseIdList,(int) stuId,null);
        if (resourceList != null) {
            resourceList.forEach(resource ->{
                if(resource.getFlow() == null || resource.getFlow().equals(0)){
                    result[0] = false;
                }
            });
        }
        Map map = new HashMap<>();
        map.put("isStudy", result[0]);
        return map;
    }

    @Override
    public Map<String, Object> stuFindIsExamSign(Integer courseId, Integer stuId) {
        Long l = examLogMapper.stuFindIsExamSign(courseId, stuId);
        Map map = new HashMap<>();
        Boolean result = l > 0 ? true : false;
        map.put("isExamSign",result);
        return map;
    }
}
